home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / ITERATOR.H < prev    next >
C/C++ Source or Header  |  1992-08-20  |  6KB  |  135 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MBN 10/10/89 -- Initial design and implementation
  13. // Updated: VDN 02/21/92 -- New lite version
  14. // Updated: JAM 08/18/92 -- modernized template syntax, remove macro hacks
  15. //                          Type##_state=>Type::IterState
  16. //
  17. // The Iterator<Container> class provides  an independent mechanism  for
  18. // maintaining the state associated with the current position of an
  19. // instance of a container class.  In this manner, multiple iterators
  20. // over the same instance of a class can be supported.  Each container
  21. // class  that  supports the current position notion has some data
  22. // structure that is used to represent the state.  This may be as simple
  23. // as a type long,  or something more  involved as with a union of of
  24. // bit fields  or even another class instance.  In addition, each 
  25. // container class  has a method  to get/set the  current   position.
  26. // Utilization of this method with the  Iterator storage will facilitate
  27. // storage  and retrieval  of the current position.
  28. // 
  29. // The container-specific  data  structure used  to   hold the current
  30. // position state is  all COOL  container  classes  will -- by  
  31. // convention --  be a nested typedef IterState defined in the container
  32. // class.  Thus, a user who includes Vector.h will declare an
  33. // Iterator<Vector<T>>, and the internal   data   structure that
  34. // maintains    the  state  will   be of  type Vector<T>::IterState (eg,
  35. // typedef long IterState).  In this manner, the Iterator<Container>
  36. // class can be parameterized over the container  class name (ie. 
  37. // Bit_Set, Vector,  etc.)  and allocate a data slot of the  appropriate
  38. // type without the user having  to know anything about internal
  39. // implementation details.
  40. // 
  41. // Each instance of a container  class has  a single slot  in the private
  42. // data section to maintain the current position. In  addition, each
  43. // container class has a  public method that  returns a reference to
  44. // this  iterator state  data structure.  The  six methods that support 
  45. // current position functionality in all of  the   container classes 
  46. // always  work  on  the current  position  as maintained in the private
  47. // data section. A user can, at any point, change the current position
  48. // state information  by utilizing this  method to get  and/or set the
  49. // current position. Finally,  each state data structure implemented in
  50. // every container class must support the assignment  of INVALID in some
  51. // manner such that a state with  this value  (defined in <COOL/misc.h>)
  52. // will cause an exception if used before set.
  53. // 
  54. // There is only one private slot in the  Iterator<Container>  class that
  55. // contains a single  element of type  Container::IterState for the
  56. // appropriate container class.  The single constructor invokes the
  57. // reset() method of the  container class to initialize the state.  The
  58. // single method  provides   a means to  efficiently access the state of
  59. // the object. All functionality that manipulates the state information
  60. // remains in the appropriate container  class. In this manner, the
  61. // Iterator<Container<T>> class remains generic.
  62.  
  63. #ifndef ITERATORH                // If we have not defined class
  64. #define ITERATORH                // Indicate class Iterator
  65.  
  66. template <class Container>
  67. class CoolIterator {
  68. public:
  69.   /*inline##*/ CoolIterator();            // constructor inits state
  70. //##  inline CoolIterator(const Container::IterState&); // construct from state
  71.   inline ~CoolIterator();     // Destructor
  72.  
  73. //##  inline CoolIterator<Container>& operator= (const Container::IterState&); // Assignment
  74.  
  75. //##  inline operator Container::IterState();        // Convert type-specific state
  76.  
  77.   CoolIterator(const Container::IterState& s) //## define here for BC++ 3.1 bug
  78.      { this->value = s; }  //##
  79.   CoolIterator<Container>& operator= (const Container::IterState& s) //## define here for BC++ 3.1 bug
  80.      { this->value = s; return *this; } //##
  81.   operator Container::IterState()  //## define here for BC++ 3.1 bug
  82.      { return this->value; } //##
  83. private:
  84.   Container::IterState value;                // Slot to hold iterator state
  85. };
  86.  
  87.  
  88. // CoolIterator<Container> -- Simple constructor for CoolIterator class
  89. // Input:            None
  90. // Output:           None
  91.  
  92. template<class Container>
  93. inline CoolIterator<Container>::CoolIterator() {}
  94.  
  95.  
  96. // CoolIterator<Container> --  Copy constructor
  97. // Input:            None
  98. // Output:           None
  99.  
  100. //## Borland doesn't allow this definition outside of class (typedef bug)
  101. //##template<class Container>
  102. //##inline CoolIterator<Container>::CoolIterator(const Container::IterState& s) {
  103. //##  this->value = s;                // Assign state
  104. //##}
  105.  
  106. // ~Iterator  -- Delete iterator, nothing.
  107.  
  108. template<class Container>
  109. inline CoolIterator<Container>::~CoolIterator() {}
  110.  
  111. // operator=  -- Assignment to store state into iterator
  112. // Input:        reference to state
  113. // Output:       none
  114.  
  115. //## Borland doesn't allow this definition outside of class (typedef bug)
  116. //##template<class Container>
  117. //##inline CoolIterator<Container>& CoolIterator<Container>::operator= (const Container::IterState& s) {
  118. //##  this->value = s;
  119. //##  return *this;
  120. //##}
  121.  
  122. // operator Container::IterState() -- Implicit conversion of Iterator<Container> to state
  123. //                             that type defined in each COOL container class.
  124. // Input:                      None
  125. // Output:                     None
  126.  
  127. //## Borland doesn't allow this definition outside of class (typedef bug)
  128. //##template<class Container>
  129. //##inline CoolIterator<Container>::operator Container::IterState () {
  130. //##  return this->value;                // Return state
  131. //##}
  132.  
  133. #endif                                          // End of ITERATORH
  134.  
  135.